/**
 * JAVACC DEMO 1.0
 * @copy right iussoft company All rights reserved.
 * @Package com.iussoft.portal.tags
 */
package com.apache.portal.tags;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * description: portal判断标签 
 * @author Hou Dayu 创建时间：2016-8-23
 */
@SuppressWarnings("serial")
public class PortalJudgeTag extends TagSupport {

	private String attrName;
	private String test;
	private String judge = "eq";//eq/start/end

	public int doStartTag() throws JspException {
		boolean b = false;
		try {
			HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
			String attrStr = String.valueOf(request.getAttribute(attrName));
			if (test.indexOf(",") != -1) {
				String str[] = test.split(",");
				for (int i = 0; i < str.length; i++) {
					String tt = str[i].trim();
					if ("start".equals(judge)) {
						if (attrStr.startsWith(tt)) {
							b = true;
							break;
						}
					} else if ("end".equals(judge)) {
						if (attrStr.endsWith(tt)) {
							b = true;
							break;
						}
					} else {
						if (attrStr.equalsIgnoreCase(tt)) {
							b = true;
							break;
						}
					}
				}
			} else if (test.indexOf("&&") != -1) {
				String str[] = test.split("&&");
				boolean flag = true;
				for (int i = 0; i < str.length; i++) {
					String tt = str[i].trim();
					if ("start".equals(judge)) {
						if (!attrStr.startsWith(tt)) {
							flag = false;
							break;
						}
					} else if ("end".equals(judge)) {
						if (!attrStr.endsWith(tt)) {
							flag = false;
							break;
						}
					} else {
						if (!attrStr.equalsIgnoreCase(tt)) {
							flag = false;
							break;
						}
					}
				}
				b = flag;
			} else if (test.indexOf("_") != -1) {//不在之内的
				String str[] = test.split("_");
				boolean flag = true;
				for (int i = 0; i < str.length; i++) {
					String tt = str[i].trim();
					if ("start".equals(judge)) {
						if (attrStr.startsWith(tt)) {
							flag = false;
							break;
						}
					} else if ("end".equals(judge)) {
						if (attrStr.endsWith(tt)) {
							flag = false;
							break;
						}
					} else {
						if (attrStr.equalsIgnoreCase(tt)) {
							flag = false;
							break;
						}
					}
				}
				b = flag;
			} else {
				if ("start".equals(judge)) {
					if (attrStr.startsWith(test)) {
						b = true;
					}
				} else if ("end".equals(judge)) {
					if (attrStr.endsWith(test)) {
						b = true;
					}
				} else {
					if (attrStr.equalsIgnoreCase(test)) {
						b = true;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (b) {
			return super.EVAL_BODY_INCLUDE;
		} else {
			return super.SKIP_BODY;
		}
	}

	public String getAttrName() {
		return attrName;
	}

	public void setAttrName(String attrName) {
		this.attrName = attrName;
	}

	public String getTest() {
		return test;
	}

	public void setTest(String test) {
		this.test = test;
	}

	public String getJudge() {
		return judge;
	}

	public void setJudge(String judge) {
		this.judge = judge;
	}
}
